/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/

#include "resetareParametrii.h"
#include "database.h"
#include <QDebug>

using namespace SQL;

ResetareParametrii::ResetareParametrii(QWidget* parent, Qt::WFlags fl)
		: KontAMabilWindow(parent, fl), Ui::resetareParametriiForm()
{
	setupUi(this);
	connect(tableview,SIGNAL(escapePressed()), defaultDatabase()->query("resetare_parametrii_masini"), SLOT(close()));
	connect(parametruLookup,SIGNAL(returnPressed()),this,SLOT(showResetareParametrii()));
	connect(tableview,SIGNAL(escapePressed()),this,SLOT(hideResetareParametrii()));
	connect(tableview,SIGNAL(escapePressed()),parametruLookup,SLOT(setFocus()));
	connect(this,SIGNAL(aboutToShow()), SLOT(hideResetareParametrii()));
	connect(this,SIGNAL(aboutToShow()),modeleLookup, SLOT(setFocus()));
	connect(defaultDatabase()->query("masini"), SIGNAL(cursorSeek(int)), SLOT(selecteazaModelul()));
	connect(this,SIGNAL(aboutToShow()), SLOT(selecteazaModelul()));
	selecteazaModelul();
}

void ResetareParametrii::selecteazaModelul()
{
	defaultDatabase()->query("modeleMasini")->locate( "id", defaultDatabase()->query("masini")->value("id_model"), true, false, false);
	defaultDatabase()->query("parametrii_masini")->bindValue("id_model",defaultDatabase()->query("masini")->value("id_model"));	
	defaultDatabase()->query("parametrii_masini")->select();
	defaultDatabase()->query("parametrii_masini")->first();
}

void ResetareParametrii::showResetareParametrii()
{
	defaultDatabase()->query("resetare_parametrii_masini")->bindValue( "id_masina", defaultDatabase()->query("masini")->value( "id" ) );
	defaultDatabase()->query("resetare_parametrii_masini")->bindValue( "id_parametru", defaultDatabase()->query("parametrii_masini")->value( "id" ) );
	defaultDatabase()->query("resetare_parametrii_masini")->select();
	parametriiFrame->setVisible( true );
	tableview->setFocus();
	modeleLookup->setEnabled(false);
	parametruLookup->setEnabled(false);
}
void ResetareParametrii::hideResetareParametrii()
{
	parametriiFrame->setVisible( false );
	parametruLookup->setEnabled(true);
	modeleLookup->setEnabled(true);
	layout()->activate();
	resize(QSize(width(), height() - parametriiFrame->height()));
}

ResetareParametrii::~ResetareParametrii()
{
}

